--Contains various useful functions

/*---------------------------------------------------------
   Name: Freeze_All_Players
   Desc: Freeze all non-spectators.
---------------------------------------------------------*/
function Freeze_All_Players( )

	for k,v in pairs( player.GetAll( ) ) do
		if ( v:Team( ) != TEAM_SPECTATOR && v:Team( ) != TEAM_CONNECTING ) then
			v:Freeze( true )
		end
	end

end

/*---------------------------------------------------------
   Name: Thaw_All_Players
   Desc: Removes frozen flag from all players.
---------------------------------------------------------*/
function Thaw_All_Players( )

	for k,v in pairs( player.GetAll( ) ) do
		if ( v:Team( ) != TEAM_SPECTATOR && v:Team( ) != TEAM_CONNECTING ) then
			v:Freeze( false )
		end
	end

end

function Spawn_All_Players( )

	for k,v in pairs( player.GetAll() ) do
		if (  v:Team( ) != TEAM_SPECTATOR && v:Team( ) != TEAM_CONNECTING ) then
			v:KillSilent( )
			v:Spawn( )
		end
	end

end

function ReSpawn_All_Players( )
	for k,v in pairs( player.GetAll() ) do
		v:KillSilent( )
		if ( v:Team( ) == TEAM_DEADCOMB ) then
			v:SetTeam( TEAM_COMBINE )
		elseif ( v:Team( ) == TEAM_DEADLAMB ) then
			v:SetTeam( TEAM_LAMBDA )
		end
		if ( v:Team( ) == TEAM_LAMBDA or v:Team( ) == TEAM_COMBINE ) then
			v:Spawn( )
		end
	end

end

function Save_All_Player_Data( )
	for k, v in pairs( player.GetAll( ) ) do
		Save_Player_Data( v )
	end
end

function Teams_Balance( )
	local lambda = team.NumPlayers( TEAM_LAMBDA )
	local combine = team.NumPlayers( TEAM_COMBINE )
	local numb_active_players = lambda + combine
	if (numb_active_players > 3 and lambda != combine ) then
		--Determine who has the greater # of players
		local who = 0
		local where = 0
		local how_much = 0
		if ( lambda > combine ) then
			who = TEAM_LAMBDA
			where = TEAM_COMBINE
			how_much = lambda - combine
		elseif ( combine > lambda ) then
			who = TEAM_COMBINE
			where = TEAM_LAMBDA
			how_much = combine - lambda
		end
		if ( how_much % 2 == 0 ) then
			--There is an even number of players move half to the other team
			how_much = how_much / 2
			local count = 0
			while ( count < how_much ) do
				local pl = table.Random( team.GetPlayers( who ) )
				pl:SetTeam( where )
				count = count + 1
			end
		else
			--There is an odd number of players move one to out and half of the remaining to the other team
			table.Random( team.GetPlayers( who ) ):SetTeam( who + 2 )
			how_much = ( how_much -  1 ) / 2
			local count = 0
			while ( count < how_much ) do
				local pl = table.Random( team.GetPlayers( who ) )
				pl:SetTeam( where )
				count = count + 1
			end
		end
	end
end

function Spawn_Balls( )
	--Spawn all the balls for the players to use
	if (GetConVar("db_dev"):GetInt() == 0) then
		number_of_balls = team.NumPlayers( TEAM_COMBINE ) + team.NumPlayers( TEAM_LAMBDA ) * 3
	else
		number_of_balls = 40
	end
	for k=0,number_of_balls + 1 do
		--Spawn a ball at a random spawn point
		local ent = ents.Create( "ent_ball_disabled" )
		ent:Spawn( )
		ent:Activate( )
		ent:SetPos(table.Random(ents.FindByClass("info_player_*")):GetPos())
	end
end

function Send_All_LUA( lua )
	for k, v in pairs( player.GetAll( ) ) do
		v:SendLua( lua )
	end
end

function Teams_Check_Out( )
	local totalAlive = team.NumPlayers( TEAM_COMBINE ) + team.NumPlayers( TEAM_LAMBDA )
	local totalDead = team.NumPlayers( TEAM_DEADCOMB) + team.NumPlayers( TEAM_DEADLAMB )
	local totalTeams = totalAlive + totalDead
	//Special conditions
	if (totalTeams == 0) then
		return false
	elseif (totalAlive == 1 and GetConVar("db_dev"):GetInt() == 1) then
		return false
	end
	//The reason to end the round prematurely
	if ( team.NumPlayers( TEAM_COMBINE ) == 0 or team.NumPlayers( TEAM_LAMBDA ) == 0 ) then
		return true
	end
	//The default action
	return false
end

function Clean_Map_Up( )
	game.CleanUpMap( )
	ReSpawn_All_Players( )
end

function Message_All( message, msgType )
	for k,v in pairs ( player.GetAll( ) ) do
		v:PrintMessage( msgType, message )
	end
end

function Round_Active( )
	if ( RoundState == 2 or RoundState == 3 or RoundState == 4 ) then
		return true
	else
		return false
	end
end
